﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CarErp.Model;

namespace CarErp.Forms.Controls
{
	public partial class CarPurchaseApplicationFilter : UserControl
	{
		public CarPurchaseApplicationFilter()
			: this(new string[] { })
		{ }

		public CarPurchaseApplicationFilter(string[] companys)
		{
			InitializeComponent();
			SetCompanys(companys);
		}

		public void SetCompanys(string[] companys)
		{
			comboBox_Company.Items.Clear();
			comboBox_Company.Items.AddRange(companys.Distinct().Concat(new[] { "任何" }).ToArray());
			comboBox_Company.SelectedIndex = 0;
		}

		public Expression<Func<CarPurchaseApplication, bool>> Filter
		{
			get
			{
				PropertyRight[] expectedProperties = null;
				if (radioButton_All.Checked)
					expectedProperties = new[] { PropertyRight.CompanyOwned, PropertyRight.LinkedFinancing };
				else if (radioButton_Company.Checked)
					expectedProperties = new[] { PropertyRight.CompanyOwned };
				else if (radioButton_Linked.Checked)
					expectedProperties = new[] { PropertyRight.LinkedFinancing };
				return model => (comboBox_Company.SelectedIndex == comboBox_Company.Items.Count - 1 || model.ApplicantCompany == comboBox_Company.SelectedItem.ToString()) &&
					expectedProperties.Contains(model.PropertyRight) &&
					model.ApplicantTime >= dateTimePicker_Buttom.Value &&
					model.ApplicantTime <= dateTimePicker_Top.Value;
			}
		}
	}
}
